from config import cfg

class DBConnection():
    def __init__(self):
        self.cfg = cfg['db']
        self.type = self.cfg['type']
        self.host = self.cfg['host']
        self.db_name = self.cfg['dbname']
        self.username = self.cfg['username']
        self.password = self.cfg['password']
        self.charset = self.cfg['charset']

        if self.type == 'mongodb':
            from db.mongodb import MongoDB
            self.db = MongoDB(self.host, self.db_name)
        else:
            print 'Please specify your database'
            self.db = None

    def insert(self, record, table_name):
        self.db.insert(record, table_name)
        
    def find(self, table, query={}):
        #cursor = 
        return self.db.find(table, query)
        #for c in cursor:
        
    def find_by_id(self, table, _id):
        from bson.objectid import ObjectId
        return self.db.find(table, {'_id': ObjectId(_id)})
                    
    def find_all(self, table):
        return self.db.find(table)